package com.hw.service.shoppingCart;

import cn.hutool.core.img.ImgUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw.entity.dto.shoppingCart.ShoppingCartDTO;
import com.hw.entity.po.order.Order;
import com.hw.entity.po.shoppingCart.ShoppingCart;
import com.hw.entity.po.sys.SysUser;
import com.hw.entity.vo.shoppingCart.ShoppingCartDataVO;
import com.hw.mapper.shoppingCart.ShoppingCartMapper;
import com.hw.model.ResponseResult;
import com.hw.service.order.OrderService;
import com.hw.service.sys.SysUserService;
import com.hw.utils.AdminUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 购物车（已点列表） Service实现类
 *
 * @author xuguang
 * @date 2021-06-29
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    @Lazy(true)
    private OrderService orderService;

    /**
     * 重置购物车(去掉订单数据)
     *
     * @return
     */
    @Override
    public ResponseResult<?> resetFood(String taskId) {
        SysUser currentUser = sysUserService.getOne(AdminUtils.getUserName());
        UpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new UpdateWrapper<>();
        shoppingCartUpdateWrapper
                .set("c_deleted", 0)
                .eq("c_user_id", currentUser.getUserId())
                .eq("c_task_id", taskId);
        // 批量删除该用户的购物车信息
        update(shoppingCartUpdateWrapper);

        // 批量删除该用户的订单信息
        UpdateWrapper<Order> orderUpdateWrapper = new UpdateWrapper<>();
        orderUpdateWrapper
                .set("c_deleted", 0)
                .eq("c_user_id", currentUser.getUserId())
                .eq("c_task_id", taskId);
        orderService.update(orderUpdateWrapper);
        return ResponseResult.success();
    }

    /**
     * 保存已点列表
     *
     * @param shoppingCartDTOs
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResponseResult<?> save(List<ShoppingCartDTO> shoppingCartDTOs) {
        // id，foodId，singlePrice，number已有，用户可获取
        // 其他随机生成

        if (shoppingCartDTOs == null) {
            return ResponseResult.error("列表不能为空!");
        }

        // 获取当前用户
        SysUser currentUser = sysUserService.getOne(AdminUtils.getUserName());

        // 先暴力删除当前用户当前订单id的购物车数据，再重新添加
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("c_deleted", 0)
                .eq("c_user_id", currentUser.getUserId())
                .eq("c_task_id", shoppingCartDTOs.get(0).getTaskId());
        remove(queryWrapper);

        for (ShoppingCartDTO dto : shoppingCartDTOs) {
            ShoppingCart po = new ShoppingCart();
            if (ObjectUtils.isNotEmpty(dto)) {
                BeanUtils.copyProperties(dto, po);
                po.setRemark(dto.getFoodRemark());
                po.setUserId(currentUser.getUserId());
                po.setShoppingCartId(UUID.randomUUID().toString());
                po.setPrice(po.getSinglePrice() * po.getNumber());
                po.setTaskId(dto.getTaskId());
            }
            baseMapper.insert(po);
        }
        return ResponseResult.success("保存成功！");
    }

    /**
     * 回显已点列表
     *
     * @return
     */
    @Override
    public ResponseResult<?> getData(String taskId) {
        // 获取当前用户
        SysUser currentUser = sysUserService.getOne(AdminUtils.getUserName());
        List<ShoppingCartDataVO> list = baseMapper.getData(currentUser.getUserId(), taskId);
        // 处理图片
//        for (ShoppingCartDataVO vo : list) {
//            if (ObjectUtils.isNotEmpty(vo.getFoodImg())) {
//                String avatarBase64 = ImgUtil.toBase64(ImgUtil.toImage(vo.getFoodImg()), "png");
//                vo.setFoodImgVO(avatarBase64);
//                vo.setFoodImg(null);
//            }
//        }
        return ResponseResult.success(list);
    }
}
